from typing import Optional

from fastapi import Depends
from fastapi import FastAPI
from fastapi.routing import APIRoute
from fastapi.routing import APIRouter

from controller.user import UserController
from controller.captcha import CaptchaController
from controller.oauth import OauthController
from controller.area import area
from controller.address import address_router

from common.utils import verif_oauth
from schemas import Response
from schemas import Token
from schemas.user import UserOutSchem

auth_router = APIRouter(tags=['认证相关'], routes=[
    APIRoute('/captcha/{uuid}', CaptchaController.image_captcha, summary='图形验证码', tags=['认证相关']),
    APIRoute('/register', UserController.register_user, methods=['post'], summary='用户注册', tags=['认证相关']),
    APIRoute('/login', UserController.login_user, methods=['post'], summary='用户登录',
             tags=['认证相关'], response_model=Response[Optional[Token]]),
    APIRoute('/sms_captcha/{mobile}', CaptchaController.sms_captcha, summary='短信验证码', tags=['认证相关']),
    APIRoute('/oauth_gitee', OauthController.goto_gitee, summary='Gitee授权', tags=['认证相关']),
    APIRoute('/oauth_gitee', OauthController.goto_gitee, summary='Gitee授权', tags=['认证相关']),
    APIRoute('/active_email', UserController.active_email, summary='激活邮箱')
])

auth_router.add_api_route('/gitee_back', OauthController.get_gitee_token, summary='Gitee登录',
                          response_model=Response[Optional[Token]])

user_router = APIRouter(tags=['用户'], dependencies=[Depends(verif_oauth)])
user_router.add_api_route('/current_user', UserController.user_info, summary='当前用户信息',
                          response_model=Response[Optional[UserOutSchem]])

user_router.add_api_route('/email', UserController.update_email, methods=['put'], summary='更新当前用户邮箱')
user_router.add_api_route('/send_email/{email}', CaptchaController.send_email, summary='发送邮件')

# api router 对象列表
router_arr = [
    auth_router,
    user_router,
    area,
    address_router
]


def register_router(app: FastAPI):
    """加入所有api router对象"""
    [app.include_router(api_router) for api_router in router_arr]


__all__ = ["register_router"]
